<?php
class ModelCommunityCommunity extends Model{
    public function getForumCatInfo(){
        $query=$this->sql_cache->get('community.forumcatinfo.' . (int)$this->config->get('config_language_id'));

        if(!$query){
            $query=$this->db->query("SELECT f.forum_id, fn.forum_name FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS fn ON (fn.forum_id=f.forum_id AND fn.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_type='0' ORDER BY f.sort_order_id ASC, fn.forum_name ASC");
            $this->sql_cache->set('community.forumcatinfo.' . (int)$this->config->get('config_language_id'),$query);
        }

        return $query->rows;
    }
    
    public function getForumInfo($forumCatId){
        $encrypt_data=md5($forumCatId);

        $results=$this->sql_cache->get('community.foruminfo.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'));

        if(!$results){
            $results=$this->db->query("SELECT f.forum_id, fn.forum_name, fn.forum_desc, f.forum_topics, f.forum_posts, f.forum_last_poster_id, f.forum_last_poster_name, f.forum_last_post_time  FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS fn ON (fn.forum_id=f.forum_id AND fn.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id='" . (int)$forumCatId . "' AND f.parent_id='0' AND f.forum_type='1' AND f.forum_enabled='1' ORDER BY f.sort_order_id ASC, fn.forum_name ASC");

            if($results->rows){
                $this->sql_cache->set('community.foruminfo.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'),$results,TRUE);
            }
        }

        $query=array();

        if($results->rows){
            $forum_id_str='';

            foreach($results->rows as $result){
                $forum_id_str.=$result['forum_id'] . ',';
            }

            $forum_id_str=rtrim($forum_id_str,',');

            $mark_time_results=$this->db->query("SELECT forum_id, mark_time FROM " . DB_PREFIX . "community_forum_track WHERE forum_id IN (" . $forum_id_str . ") AND user_id='" . (int)$this->customer->getForumUserId() . "'");

            $i=0;

            foreach($results->rows as $result){
                $query[]=$result;

                foreach($mark_time_results->rows as $mark_time_result){
                    if($mark_time_result['forum_id']==$result['forum_id']){
                        $query[$i]['mark_time']=$mark_time_result['mark_time'];
                    }
                }
                $i++;
            }
        }

        return $query;
    }
    
    public function getSubForumInfo($forumCatId,$parentForumId){
        $encrypt_data=md5($forumCatId . $parentForumId);

        $results=$this->sql_cache->get('community.subforuminfo.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'));

        if(!$results){
            $results=$this->db->query("SELECT f.forum_id, fn.forum_name, f.forum_posts, f.forum_last_post_time FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS fn ON (fn.forum_id=f.forum_id AND fn.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id='" . (int)$forumCatId . "' AND f.parent_id='" . (int)$parentForumId . "' AND f.forum_enabled='1' ORDER BY f.sort_order_id ASC, fn.forum_name ASC");

            if($results->rows){
                $this->sql_cache->set('community.subforuminfo.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'),$results);
            }
        }

        if($results->rows){
            $forum_id_str='';

            foreach($results->rows as $result){
                $forum_id_str.=$result['forum_id'] . ',';
            }

            $forum_id_str=rtrim($forum_id_str,',');

            $mark_time_results=$this->db->query("SELECT forum_id, mark_time FROM " . DB_PREFIX . "community_forum_track WHERE forum_id IN (" . $forum_id_str . ") AND user_id='" . (int)$this->customer->getForumUserId() . "'");

            $query=array();

            $i=0;

            foreach($results->rows as $result){
                $query[]=$result;

                foreach($mark_time_results->rows as $mark_time_result){
                    if($mark_time_result['forum_id']==$result['forum_id']){
                        $query[$i]['mark_time']=$mark_time_result['mark_time'];
                    }
                }
                $i++;
            }
        }else{
            $query=$results->rows;
        }

        return $query;
    }

    public function getOnlineUserTotal(){
        $query=$this->sql_cache->get('community.onlineusertotal');

        if(!$query){
            $query=$this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "community_sessions WHERE session_last_visit_time>=" . (int)time() . "-" . (int)($this->config->get("online_time_span")*60) . " AND session_viewonline='1'");

            $this->sql_cache->set('community.onlineusertotal',$query, TRUE, (60*60));
        }

        return $query->row['total'];
    }

    public function getOnlineUsers(){
        $query=$this->sql_cache->get('community.onlineusers');

        if(!$query){
            $query=$this->db->query("SELECT u.user_id, u.user_type, u.username FROM " . DB_PREFIX . "community_sessions AS s LEFT JOIN " . DB_PREFIX . "community_users AS u ON (s.session_user_id=u.user_id) WHERE s.session_last_visit_time>=" . (int)time() . "-" . (int)($this->config->get("online_time_span")*60) . " AND s.session_viewonline='1'");

            $this->sql_cache->set('community.onlineusers',$query, TRUE, (60*60));
        }

        return $query->rows;
    }

    public function UpdateOnlinePeak($online_peak_num){
        $this->db->query("UPDATE " . DB_PREFIX . "setting SET `value`='" . $this->db->escape(serialize(array($online_peak_num,time()))) . "' WHERE `key`='most_user_online'");
    }
}
?>